package com.dogstudio.mygame.config;

import com.dogstudio.mygame.view.Card;

import static com.dogstudio.mygame.view.GamePanel.DEFAULT_COLUMN;
import static com.dogstudio.mygame.view.GamePanel.DEFAULT_ROW;


public class GameScore {
    public static final int NUMBER_4 = 1;
    public static final int NUMBER_8 = 2;
    public static final int NUMBER_16 = 3;
    public static final int NUMBER_32 = 4;
    public static final int NUMBER_64 = 5;
    public static final int NUMBER_128 = 6;
    public static final int NUMBER_256 = 7;
    public static final int NUMBER_512 = 8;
    public static final int NUMBER_1024 = 9;
    public static final int NUMBER_2048 = 10;

    /**
     * 游戏成功
     */
    public static boolean isGameSuccess(Card[][] mGroup) {
        if (mGroup != null) {
            for (int i = 0; i < DEFAULT_ROW; i++) {
                for (int j = 0; j < DEFAULT_COLUMN; j++) {
                    if (mGroup[i][j].getNumber() == 2048) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     *
     */
    public static boolean isGameMerge(Card[][] mGroup) {
        boolean isCanMerge = true;
        for (int i = 0; i < DEFAULT_ROW; i++) {
            for (int j = 0; j < DEFAULT_COLUMN; j++) {
                isCanMerge = isCanMerge(mGroup, i, j);
                if (isCanMerge) {
                    return true;
                }
            }
        }
        return isCanMerge;
    }

    private static boolean isCanMerge(Card[][] mGroup, int i, int j) {
        if (i >= 0 && i < DEFAULT_ROW && j >= 0 && j < DEFAULT_COLUMN) {
            Card cur = mGroup[i][j];
            Card other;
            // 判断是否可以向左合并
            if (j < DEFAULT_COLUMN - 1) {
                other = mGroup[i][j + 1];
                if (other.getNumber() == cur.getNumber()) {
                    return true;
                }
            }
            //判断是否可以向上合并
            if (i < DEFAULT_ROW - 1) {
                other = mGroup[i + 1][j];
                if (other.getNumber() == cur.getNumber()) {
                    return true;
                }
            }
            // 判断是否可以向右合并
            if (j > 0) {
                other = mGroup[i][j - 1];
                if (other.getNumber() == cur.getNumber()) {
                    return true;
                }
            }
            // 判断是否可以向下合并
            if (i > 0) {
                other = mGroup[i - 1][j];
                if (other.getNumber() == cur.getNumber()) {
                    return true;
                }
            }
        }
        return false;
    }


    public static int addGameScore(int number) {
        int score = 0;
        switch (number) {
            case 4:
                score = NUMBER_4;
                break;
            case 8:
                score = NUMBER_8;
                break;
            case 16:
                score = NUMBER_16;
                break;
            case 32:
                score = NUMBER_32;
                break;
            case 64:
                score = NUMBER_64;
                break;
            case 128:
                score = NUMBER_128;
                break;
            case 256:
                score = NUMBER_256;
                break;
            case 512:
                score = NUMBER_512;
                break;
            case 1024:
                score = NUMBER_1024;
                break;
            case 2048:
                score = NUMBER_2048;
                break;
        }
        return score;
    }


}
